﻿using System.IO;
using SSAT.Utils;

namespace SSAT.ATSDriver
{
    internal sealed class Report
    {
        private ReportDetails _atsReport;
        private ReportDetails _msReport;
        private bool _hasReport;

        public bool HasReport
        {
            get { return _hasReport; }
        }

        public ReportDetails AtsReport
        {
            get { return _atsReport; }
        }

        public ReportDetails MSReport
        {
            get { return _msReport; }
        }

        public Report(ReportDetails atsReport, ReportDetails msReport)
        {
            _atsReport = atsReport;
            _msReport = msReport;
            WriteThatProblemNotFoundInCaseOfNull();
        }

        private void WriteThatProblemNotFoundInCaseOfNull()
        {
            _hasReport = true;
            if (_atsReport == null)
            {
                _hasReport = false;
                _atsReport = new ReportDetails();
                _atsReport.Write(Resources.NotFoundAnyProblems);
                _msReport = _atsReport;
            }
        }

        public void Save(Stream stream)
        {
            SaveReportToStream(stream, _atsReport);   
        }

        public void Save(string file)
        {
            Save(new FileStream(file, FileMode.Create));
        }

        public void SaveMsReport(Stream stream)
        {
            SaveReportToStream(stream, _msReport);
        }

        public void SaveMsReport(string file)
        {
            SaveMsReport(new FileStream(file, FileMode.Create));
        }

        private static void SaveReportToStream(Stream stream, ReportDetails report)
        {
            Verification.CheckArgumentForNull(stream, "stream");
            Verification.CheckArgumentForNull(report, "report");

            using (StreamWriter writer = new StreamWriter(stream))
            {
                writer.WriteLine(report.GetContent());
            }
        }
    }
}
